package com.bqtv.sport.ui.home.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewbinding.ViewBinding
import com.bqtv.sport.R
import com.bqtv.sport.adapter.LiveIndexCornerAdapter
import com.bqtv.sport.adapter.LiveIndexHandicapAdapter
import com.bqtv.sport.adapter.LiveIndexTotalGoalsAdapter
import com.bqtv.sport.adapter.LiveIndexWLDAdapter
import com.bqtv.sport.app.AppActivity
import com.bqtv.sport.app.AppFragment
import com.bqtv.sport.databinding.FragmentLiveIndexBinding
import com.bqtv.sport.ui.main.LiveIndexMoreActivity
import com.chad.library.adapter4.BaseQuickAdapter


class LiveIndexFragment : AppFragment<FragmentLiveIndexBinding, AppActivity<ViewBinding>>() {

    private lateinit var handicapAdapter: LiveIndexHandicapAdapter
    private lateinit var wldAdapter: LiveIndexWLDAdapter
    private lateinit var goalsAdapter: LiveIndexTotalGoalsAdapter
    private lateinit var cornerAdapter: LiveIndexCornerAdapter

    companion object {
        private const val INTENT_KEY_TYPE = "type"
        fun newInstance(): LiveIndexFragment {
            val fragment = LiveIndexFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }

        fun newInstance(type: String?): LiveIndexFragment {
            val fragment = LiveIndexFragment()
            val bundle = Bundle()
            bundle.putString(INTENT_KEY_TYPE, type)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getViewBinding() = FragmentLiveIndexBinding.inflate(layoutInflater)

    override fun initView() {

    }

    override fun initData() {
        binding.recycler.layoutManager = LinearLayoutManager(requireContext())
        handicapAdapter = LiveIndexHandicapAdapter()
        wldAdapter = LiveIndexWLDAdapter()
        goalsAdapter = LiveIndexTotalGoalsAdapter()
        cornerAdapter = LiveIndexCornerAdapter()
        binding.recycler.adapter = handicapAdapter

        var list: MutableList<String> = ArrayList()
        for (i in 0..10) {
            list.add("")
        }
        handicapAdapter.submitList(list)
        wldAdapter.submitList(list)
        goalsAdapter.submitList(list)
        cornerAdapter.submitList(list)

        initListener()
    }

    private fun initListener() {
        handicapAdapter.setOnItemClickListener{adapter, view, position ->
            LiveIndexMoreActivity.start(requireActivity(), 1)
        }
        wldAdapter.setOnItemClickListener{adapter, view, position ->
            LiveIndexMoreActivity.start(requireActivity(), 2)
        }
        goalsAdapter.setOnItemClickListener{adapter, view, position ->
            LiveIndexMoreActivity.start(requireActivity(), 3)
        }
        cornerAdapter.setOnItemClickListener{adapter, view, position ->
            LiveIndexMoreActivity.start(requireActivity(), 4)
        }
        binding.tvHandicap.setOnClickListener(this)
        binding.tvWinLoseDraw.setOnClickListener(this)
        binding.tvTotalGoals.setOnClickListener(this)
        binding.tvCorner.setOnClickListener(this)
    }

    override fun onClick(view: View) {
        super.onClick(view)
        when (view) {
            binding.tvHandicap -> {
                setButtonClear()
                binding.tvHandicap.setTextColor(resources.getColor(R.color.white))
                binding.tvHandicap.setBackgroundResource(R.drawable.button_theme)
                binding.llHandicap.visibility = View.VISIBLE
                binding.recycler.adapter = handicapAdapter
            }

            binding.tvWinLoseDraw -> {
                setButtonClear()
                binding.tvWinLoseDraw.setTextColor(resources.getColor(R.color.white))
                binding.tvWinLoseDraw.setBackgroundResource(R.drawable.button_theme)
                binding.llWLD.visibility = View.VISIBLE
                binding.recycler.adapter = wldAdapter
            }

            binding.tvTotalGoals -> {
                setButtonClear()
                binding.tvTotalGoals.setTextColor(resources.getColor(R.color.white))
                binding.tvTotalGoals.setBackgroundResource(R.drawable.button_theme)
                binding.llTotalGoals.visibility = View.VISIBLE
                binding.recycler.adapter = goalsAdapter
            }

            binding.tvCorner -> {
                setButtonClear()
                binding.tvCorner.setTextColor(resources.getColor(R.color.white))
                binding.tvCorner.setBackgroundResource(R.drawable.button_theme)
                binding.llTotalGoals.visibility = View.VISIBLE
                binding.recycler.adapter = cornerAdapter

            }
        }
    }

    private fun setButtonClear() {
        binding.tvHandicap.setTextColor(resources.getColor(R.color.text_222222))
        binding.tvHandicap.setBackgroundResource(R.drawable.bg_round_ffffff)
        binding.tvWinLoseDraw.setTextColor(resources.getColor(R.color.text_222222))
        binding.tvWinLoseDraw.setBackgroundResource(R.drawable.bg_round_ffffff)
        binding.tvTotalGoals.setTextColor(resources.getColor(R.color.text_222222))
        binding.tvTotalGoals.setBackgroundResource(R.drawable.bg_round_ffffff)
        binding.tvCorner.setTextColor(resources.getColor(R.color.text_222222))
        binding.tvCorner.setBackgroundResource(R.drawable.bg_round_ffffff)
        binding.llHandicap.visibility = View.GONE
        binding.llWLD.visibility = View.GONE
        binding.llTotalGoals.visibility = View.GONE
    }
}


